package com.zzy.demo.action.upload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 
 * @author guokaige
 *
 */
@Controller
@RequestMapping("/vue")
public class VueUploadAction {
	@Value("${file.upload.path}")
	private String filePath;
	
	
	 @RequestMapping("/index")
	 public String uploadIndex(HttpServletRequest request, ModelMap model) {
	    	//企业基本信息
	        return "/vue/vue";
	    }
	 
		@RequestMapping(value = "/batchUpload", method = RequestMethod.POST)
		@ResponseBody
		public String upload(HttpServletResponse response, HttpServletRequest request) throws IOException {
			String responseStr = "000";
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();

			// 文件保存路径 ctxPath本地路径
			String ctxPath = filePath;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			String ymd = sdf.format(new Date());
			ctxPath += File.separator + ymd + File.separator;
			System.out.println("ctxpath=" + ctxPath);
			// 创建文件夹
			File file = new File(ctxPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			String fileName = null;
			for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
				// 上传文件
				MultipartFile mf = entity.getValue();
				fileName = mf.getOriginalFilename();// 获取原文件名
				System.out.println("filename=" + fileName);
				File uploadFile = new File(ctxPath + fileName);
				try {
					FileCopyUtils.copy(mf.getBytes(), uploadFile);
					responseStr = "上传成功";
				} catch (IOException e) {
					responseStr = "上传失败";
					e.printStackTrace();
				}
			}
			return responseStr;
		}
	 
	
}
